/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.component;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;

/**
 *
 * @author Kevin
 */
public class DateCombobox extends Box
{
    private static final int YEAR_THAT_THE_APPLICATION_WAS_CREATED = 2011;
    
    private final JComboBox cmbDay;
    private final JComboBox cmbMonth;
    private final JComboBox cmbYear;
    
    private final Object[] days;
    private final Object[] months;
    private final Object[] years;
    
    public DateCombobox()
    {
        super(BoxLayout.X_AXIS);
        
        days = new Object[31];
        
        for (int i = 0; i < 31; i++)
            days[i] = i + 1;
        
        months = new Object[] {
            "Januari", "Februari", "Maart",
            "April", "Mei", "Juni",
            "Juli", "Augustus", "September",
            "Oktober", "November", "December"
        };
        
        int currentYear = GregorianCalendar.getInstance().get(Calendar.YEAR);
        
        years = new Object[
            currentYear - YEAR_THAT_THE_APPLICATION_WAS_CREATED + 1
        ];
        
        for (int i = 0, year = YEAR_THAT_THE_APPLICATION_WAS_CREATED; year <= currentYear; i++, year++)
            years[i] = year;
        
        cmbDay = new JComboBox(days);
        cmbMonth = new JComboBox(months);
        cmbYear = new JComboBox(years);
        
        add(cmbDay);
        add(cmbMonth);
        add(cmbYear);        
    }
    
    public Date getDate()
    {
        int day   = (Integer) cmbDay.getSelectedItem();
        int month = cmbMonth.getSelectedIndex();
        int year  = (Integer) cmbYear.getSelectedItem();
        
        Calendar c = GregorianCalendar.getInstance();
        c.set(year, month, day);
        
        return c.getTime();
    }
    
    public void setDate(Date d)
    {
        Calendar c = GregorianCalendar.getInstance();
        c.setTime(d);

        int year = c.get(Calendar.YEAR);
        
        if (year < YEAR_THAT_THE_APPLICATION_WAS_CREATED)
        {
            throw new IllegalArgumentException("Year may not be lower than " +
                                               "the year that the application " +
                                               "was created in (" + 
                                                YEAR_THAT_THE_APPLICATION_WAS_CREATED +
                                               ")");
        }
        
        if (year > Calendar.getInstance().get(Calendar.YEAR))
        {
            throw new IllegalArgumentException("Year of the date may not " +
                                               "exceed the current year");
        }
        
        int dayIndex = c.get(Calendar.DAY_OF_MONTH);
        int monthIndex = c.get(Calendar.MONTH);
        int yearIndex = year - YEAR_THAT_THE_APPLICATION_WAS_CREATED;
        
        cmbDay.setSelectedIndex(dayIndex - 1);
        cmbMonth.setSelectedIndex(monthIndex);
        cmbYear.setSelectedIndex(yearIndex);
    }
}
